import loader from '@utils/loader';
import { concat, findIndex, forEach, shuffle } from 'lodash';
import { reactive, ref } from 'vue';

let modeReturn: modeReturn = [];
const cardPile = reactive<cardPile>([]);

const mode: useIdentityReturn = {
    listen(config) {
        let num = 0;
        forEach(config.eHubs, eHub => {
            const { on } = eHub;
            on('setHeroDone', name => {
                num++;
                if (num === config.playerNum) {
                    console.log('所有玩家选将完毕');
                    // const { emit } = config.eHubs[0];
                    // config.eHubs[0].emit('round');
                }
            });
        });
    },
    setIdentitys(config) {
        let identitys: identity[];
        const o = {
            zhu: 7,
            zhong: 6,
            nei: 7,
            fan: 5,
            unknown: 5,
        };
        switch (config.playerNum) {
            case 2:
                identitys = ['zhu', 'fan'];
                break;
            case 3:
                identitys = ['zhu', 'nei', 'fan'];
                break;
            case 5:
                identitys = ['zhu', 'zhong', 'nei', 'fan', 'fan'];
                break;
            default:
                identitys = ['zhu', 'zhong', 'zhong', 'nei', 'fan', 'fan', 'fan', 'fan'];
                break;
        }
        identitys = shuffle(identitys);
        forEach(identitys, i => {
            modeReturn.push({
                seat: ref(-1),
                identity: ref(i),
                setHeroNum: ref(o[i]),
            });
        });
    },
    setSeats(config) {
        const index = findIndex(modeReturn, i => i.identity.value === 'zhu');
        const splice = modeReturn.splice(0, index);
        const splice2 = config.eHubs.splice(0, index);
        modeReturn = concat(modeReturn, splice);
        config.eHubs = concat<eHub>(config.eHubs, splice2);
        forEach(modeReturn, (i, k) => {
            i.seat.value = k;
            // config.eHubs[k].data.seat = k;
            // console.log(config.eHubs[k].data);
        });
    },
    async setCardPile(config) {
        const _cardPile: cardPile = await loader('card', config.cardPile);
        cardPile.push(...shuffle(_cardPile));
    },
    setHeros(config) {
        forEach(config.eHubs, eHub => {
            eHub.emit('setHero', null, name => {
                num++;
                if (num === config.playerNum) {
                    console.log('所有玩家选将完毕');
                    // const { emit } = config.eHubs[0];
                    // config.eHubs[0].emit('round');
                }
            });
        });
    },
    prepare(config) {
        forEach(config.eHubs, eHub => {
            eHub.emit('prepare', modeReturn);
        });
    },
    async start(config) {
        mode.listen(config);
        mode.setIdentitys(config);
        mode.setSeats(config);
        await mode.setCardPile(config);
        mode.prepare(config);
        mode.setHeros(config);
    },
};
export default mode;
